// pages/easyExercise/easyExercise.js
const db = wx.cloud.database();
import common from "../../utils/public.js"
var languageUtil = require('../../utils/languageUtils')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:'每日练习',
    m:'00',
    s:'00',
    setInter:'',
    startTime:'',
    exercise:[],
    studentAnswer:[ ],
    times:0,
    inputIsOver:false
  },
  switchLanguage() {
    //切换当前版本，即修改公共变量中的version
    languageUtil.changLanguage()
    this.initLanguage()
},
//初始化语言
  initLanguage() {
    //获取当前小程序语言版本所对应的字典变量
    var lang = languageUtil.languageVersion()
    this.setData({
      content: lang
    })
  },

  init(){
    this.initLanguage()
    var arr = new Array(20)
    this.setData({
      studentAnswer:arr
    })
    this.queryTime()
    this.setData({
      startTime:Date.now()
    })   
  },
  //再来一次
  tryAgain(){
    this.endTime()
    var times = this.data.times +1;
    this.setData({
      times:times,
      m:'00',
      s:'00',
      setInter:'',
      startTime:'',
      inputIsOver:false,
      studentAnswer:[]
    })
    this.init()
    var skip = this.data.exercise.length*times;
    this.getProblem('integer',20,skip)
  },
  /**
   * 利用云函数从数据库调用习题
   * type:数据库集合名
   * limit：单次显示的题数
   * skip：再来一次
   */
  getProblem(type='integer',limit=20,skip=0){
    wx.cloud.callFunction({
      name:'getProblem',
      data:{
        type:type,
        limit:limit,
        skip:skip
      }
    }).then(res=>{
      this.setData({
        exercise:res.result.data
      })
    })
  },

  //从输入框中获取所输入答案
  getAnswer(event){
    var index = event.currentTarget.id

    let answerList = this.data.studentAnswer

    answerList[index] = event.detail.value
    this.setData({
      studentAnswer:answerList
    }) 
  },
  submit(){
    this.endTime()
    this.setData({
      inputIsOver:true
    })

    let rightAnswer = this.data.exercise
    var score = 0
    //计算分数
    rightAnswer.forEach((item,index)=>{
      if(item.answer == this.data.studentAnswer[index]){
        score = score + 5
      }
    })
    //修改时间戳
    var date = common.setDate(this.data.startTime,"Y年m月d日") 
    //上传历史记录
    db.collection('history').add({
      data:{
        time:date,
        type:this.data.type,
        usetime:this.data.m+':'+this.data.s,
        score:score+'分'
      }
    })
    //提示
    var that = this
    wx.showModal({
      content:'本次练习已结束\r\n用时：'+this.data.m+':'+this.data.s+'，得分：'+score+'分\r\n是否再练习一次？',
      cancelText:'查看详情',
      confirmText:'再来一组',
      success(res){
        if(res.confirm){
          that.tryAgain()
        }
      }
    })
  },

  queryTime(){
    const that = this
    var min = this.data.m
    var sec = this.data.s
    that.data.setInter =  setInterval(function(){
      sec++;
      if(sec >= 60){
        sec = 0
        that.setData({
          s:'00'
        })
        min++
        that.setData({
          m:(min < 10 ? '0'+min : min)
        })
      }else{
        that.setData({
          s:(sec < 10 ? '0'+sec : sec)
        })
      }
    },1000)
  },

  endTime(){
    var that =this 
    clearInterval(that.data.setInter)
  },

  onLoad: function (options) {
    this.init()
    this.getProblem()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})